//
//  VisitorView.swift
//  MagicWeibo
//
//  Created by 李明禄 on 15/10/28.
//  Copyright © 2015年 http://blog.csdn.net/netluoriver. All rights reserved.
//

import UIKit

protocol VisitorViewDelegate: NSObjectProtocol {
    func visitorViewWillLogin()
    func visitorViewWillRegister()
}

class VisitorView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //定义代理属性
    weak var delegate: VisitorViewDelegate?
    
    //访客视图信息
    func setupInfo(isHome: Bool, imageName: String, message: String) {
        messageLabel.text = message
        
        translateIcon.image = UIImage(named: imageName)
        
        if !isHome {
            houseIcon.hidden = true
            sendSubviewToBack(maskIcon)
        }else {
            startAnimation()
        }
    }
    
    
    
    
    private func setupUI() {
        //添加控件
        addSubview(translateIcon)
        addSubview(maskIcon)
        addSubview(houseIcon)
        addSubview(LoginButton)
        addSubview(registerButton)
        addSubview(messageLabel)
        
        //设置布局
        //一定要记得先取消autoresizing布局

        //转场动画的约束
        translateIcon.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: translateIcon, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: translateIcon, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //遮罩图层
        maskIcon.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil , views: ["mask" : maskIcon]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(-30)-[register]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil , views: ["mask" : maskIcon,"register" : registerButton]))
        
        
        //房子约束
        houseIcon.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: houseIcon, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: translateIcon, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIcon, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: translateIcon, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        
        // 3> 描述文字
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: translateIcon, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: translateIcon, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        
        //登录按钮
        LoginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 15))
        
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 15))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
    
    //设置转场动画
    func startAnimation() {
        let aim = CABasicAnimation(keyPath: "transform.rotation")
        aim.toValue = 2 * M_PI
        aim.repeatCount = MAXFLOAT
        aim.duration = 20
        
        //防止程序动画不继续播放
        aim.removedOnCompletion = false
        translateIcon.layer.addAnimation(aim, forKey: nil)
        
        
    }
    
    //MARK: 懒加载界面控件
    //转动图像
    private lazy var translateIcon: UIImageView = {
        let imageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return imageView
    }()
    
    //遮罩图层
    private lazy var maskIcon: UIImageView = {
        let imageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return imageView
    }()
    
    
    //房子
    private lazy var houseIcon: UIImageView = {
        let imageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return imageView
    }()
    
    
    // 描述文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 0
        label.sizeToFit()
        
        return label
    }()

    
    //登录
    private lazy var LoginButton: UIButton = {
        let button = UIButton()
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "clickLoginButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    
    //注册
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "clickRegistrButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.sizeToFit()
        
        return button
    }()
    
    
    //MARK: 通知代理做事情 
    func clickLoginButton() {
       delegate?.visitorViewWillLogin()
    }
    
    func clickRegistrButton() {
        delegate?.visitorViewWillRegister()
    }

}
